<?php
require_once(ROOT_ADMIN_MODELS.DS.'Models_Group.php');
require_once(ROOT_ADMIN_MODELS.DS.'Models_Base.php');
require_once(ROOT_ADMIN_MODELS.DS.'Models_Log.php'); 
require_once(ROOT_ADMIN_OBJECT.DS.'Objs_Group.php'); 
require_once(ROOT_ADMIN_FORMS.DS.'Forms_Group.php');
require_once(ROOT_ADMIN_FORMS.DS.'Forms_Base.php');
require_once(ROOT_LIBRARY_UTILITY.DS.'utility.php');
require_once(ROOT_ADMIN_CONTROLLERS.DS.'BaseController.php');
class Admin_GroupController extends BaseController {	
	
	public function preDispatch()
	{
		parent::preDispatch();	
		if(!$this->hasPrivilege())
			$this->_redirect ("/admin/error/permission");
	}
	
	public function indexAction()
	{
		try 
		{	
			$auth = Zend_Auth::getInstance();
			$infoUser = $auth->getIdentity();
			$this->view->userName = $infoUser->userName;
			$baseurl=$this->_request->getbaseurl();		
			$this->view ->headTitle("UIT-News Supporter");		
			$request = $this->getRequest();
			$this->view->headMeta()->setHttpEquiv('Content-Type', 'text/html;charset=utf-8');
			$this->view->headMeta()->appendName('keywords', 'view,specific,keywords');
			$this->view->headLink()->appendStylesheet($baseurl."/public/css/test.css");
			$md=new Model_Group();
			$this->view->data=$md->listGroup();	
		}
		catch(Exception $ex)
		{
			$this->view->errMsg = $ex->getMessage();
			Utility::log($ex->getMessage(), $ex->getFile(), $ex->getLine());
		} 
		
				
	
	}
	public function editAction(){
		try 
		{
			$this->view ->headTitle("UIT-News Supporter");
			$md=new Model_Group();
			$id=$this->_request->getParam('id');
			require_once(ROOT_ADMIN_MODELS.DS.'Models_Group_Privilege.php');
			$mdSP = new Models_Group_Privilege();
			if($this->_request->isPost())
			{
				require_once(ROOT_ADMIN_OBJECT.DS.'Objs_Group_Privilege.php');
				$objGP = new Obj_Group_Privilege();
				$objGP->id = "NULL";
				$objGP->group_id = $id;
				$objGP->status = 1;
				$privileges = $this->_request->getParam('privileges');
				$mdSP->deleteByGroup($id);
				if($privileges)
				foreach($privileges as $row)
				{
					$objGP->privilege_id = $row;
					$mdSP->_insert($objGP);
				}
				$form=new Forms_Group();
				$form->_requestToForm($this);
				$md->_update($form->obj);
				Models_Log::insert($this->view->user->id, "act_update_Group",$form->obj->id);

			}
			$this->view->arrGP = $mdSP->getarrGroupPrivilege($id, "");
			$this->view->arrcontrollername = $mdSP->getarrControllname($id);
			$this->view->obj=$md->_getByKey($id);
		}
		 catch (Exception $ex) 
		{
			$this->view->errMsg = $ex->getMessage();
			Utility::log($ex->getMessage(), $ex->getFile(), $ex->getLine());
		}	
	
	}
	public function addAction()
	{
		$this->view ->headTitle("UIT-News Supporter");
		try 
		{
			$md=new Model_Group();
			
			if($this->_request->isPost())
			{
				
				
				$form=new Forms_Group();
				$form->_requestToForm($this);				
				$form->validate(INSERT);
				$md->_insert($form->obj);				
				Models_Log::insert("","add_group_action","");
				$this->_redirect("/admin/group/index");
				Models_Log::insert("aa", "act_add_Group","s");
			}
		}
		catch(Exception $ex)
		{
			$this->view->errMsg = $ex->getMessage();
			Utility::log($ex->getMessage(), $ex->getFile(), $ex->getLine());
		}
	}
	
}